/**
 * 
 */
package sudoku.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import sudoku.images.ImageLoader;
import sudoku.util.BrowserControl;
import sudoku.util.Language;


/**
 * @author Romet
 *
 */
public class HelpUI extends JFrame implements MouseListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private volatile Language language;
	
	private JLabel link;
	
	/**
	 * Teeb mängu abi akna.
	 * @param language keele klassi isend
	 */
	public HelpUI(Language language){
		this.language = language;
	}

	public void drawWindow() {
		if (isShowing())
			requestFocus();
		else {
			setTitle(language.getSudoku_abi_menu());
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			JTabbedPane tabs = new JTabbedPane();
			for (int tabnumber = 0; tabnumber < 9; tabnumber++) {
				int i = 0;
				JPanel p = new JPanel(new GridBagLayout());
				GridBagConstraints c = new GridBagConstraints();
				String[] s = language.getHelp_message(tabnumber).split("\n");
				for (; i < s.length; i++) {
					c.gridx = 0;
					c.gridy = i;
					c.anchor = GridBagConstraints.LINE_START;
					JLabel label = new JLabel(s[i]);
					if (s[i].startsWith(" http")) {
						link = label;
						link.setForeground(Color.BLUE);
						link.addMouseListener(this);
						p.add(link, c);
					}
					else if (s[i].startsWith(" [b]")) {
						Font font = label.getFont();
						label.setFont(font.deriveFont(Font.BOLD, font.getSize()+5));
						label.setText(s[i].substring(4));
						p.add(label, c);
					}
					else if (s[i].startsWith(" [image]")) {
						label.setIcon(new ImageIcon(ImageLoader.getImage(s[i].substring(8))));
						label.setText("");
						p.add(label, c);
					}
					else if (s[i].startsWith(" [red]")) {
						Font font = label.getFont();
						label.setFont(font.deriveFont(Font.BOLD, font.getSize()+3));
						label.setForeground(Color.RED);
						label.setText(s[i].substring(6));
						p.add(label, c);
					}
					else
						p.add(label, c);
				}
				JScrollPane scrollPane = new JScrollPane(p);
				scrollPane.setWheelScrollingEnabled(true);
				scrollPane.setPreferredSize(new Dimension(600, 500));
				tabs.add(language.getHelp_tabtitle(tabnumber), scrollPane);
			}
			
			add(tabs, BorderLayout.CENTER);
			setIconImage(ImageLoader.getImage("icon.gif"));
			pack();
			setVisible(true);
		}
	}

	public void mouseClicked(MouseEvent e) {
		BrowserControl.displayURL(link.getText().substring(1));
		dispose();
	}

	public void mouseEntered(MouseEvent e) {
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	public void mouseExited(MouseEvent e) {
		setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void setLanguage(Language language) {
		this.language = language;
	}
}
